<template>
  <basic-dialog
    :visible.sync="companyVisible"
    :title="'选择收货公司'"
    :width="'70%'"
    :handleClose="handleCloseDialog"
    :handleOpen="handleOpenDialog"
    :showFooter="false"
  >
    <el-form
      inline
      ref="formData"
      :model="formData"
    >
      <el-form-item label="收货公司" prop="receiveCompanyName">
        <el-input
          v-model.trim="formData.receiveCompanyName"
          placeholder="请输入收货公司"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model.trim="formData.productName"
          placeholder="请输入商品名称"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getTableData">查询</el-button>
        <el-button type="primary" :disabled="noSelect" @click="handleConfirm">确认收货公司</el-button>
      </el-form-item>
    </el-form>
    <BasicTable
      showSelectCol
      :showTotal="false"
      :dataSource="tableInfo.list"
      :columns="tableInfo.columns"
      :loading="tableInfo.loading"
      :tableHeight="480"
      :selectionChange="selectionChange"
    >
    </BasicTable>
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicTable from '@/components/skusTable';
import { queryTransferCompanyService } from '@api/stock/stockOverflow';

export default {
  name: 'SetCompanyDialog', // 设置收货公司
  components: {
    BasicDialog,
    BasicTable,
  },
  props: {
    companyVisible: {
      type: Boolean,
      default: false
    },
    selectIds: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      formData: {
        receiveCompanyName: '',
        productName: '',
        idList: []
      },
      tableInfo: {
        list: [],
        selection: [],
        columns: [],
        loading: false
      },
      // 根据计量单位数量小数位默认“0时为1,1时为0.1,2时为0.01,3时为0.001”
      saleMinMap: {
        0: '1',
        1: '0.1',
        2: '0.01',
        3: '0.001',
      },
    };
  },
  computed: {
    noSelect () {
      return this.tableInfo.selection.length === 0;
    }
  },
  methods: {
    initTableColumn () {
      this.tableInfo.columns = [
        {
          title: 'SKU编码',
          key: 'skuNo',
        },
        {
          title: '商品名称',
          key: 'productName',
        },
        {
          title: '规格',
          key: 'specialDetail',
        },
        {
          title: '计量单位',
          key: 'unitName',
        },
        {
          title: '件装数量',
          key: 'pieceAmount',
        },
        {
          title: '可售库存数量',
          key: 'canSendAmount',
        },
        {
          title: '收货公司',
          key: 'receiveCompanyName',
          width: 100
        },
        {
          title: '可转货数量',
          key: 'canTransferNum',
          width: 100
        },
        {
          title: '转货数量',
          key: 'transferNum',
          width: 160,
          render: (h, { row }) => {
            return <el-input-number controls={ false } v-model={row.transferNum} min={0} max={row.canSendAmount} precision={row.decimalPlaces} />
          }
        },
        {
          title: '本区域近三月销量',
          key: 'areaThreeMonthsOut',
          width: 120
        },
        {
          title: '本区域近一年销量',
          key: 'transferAreaYearOut',
          width: 120
        },
        {
          title: '收货区域近三月销量',
          key: 'receiveAreaThreeMonthsOut',
          width: 130
        },
        {
          title: '收货区域近一年销量',
          key: 'receiveAreaYearOut',
          width: 120
        },
        {
          title: '收货区域可售库存',
          key: 'receiveAreaCanSendAmout',
          width: 120
        },
        {
          title: '收货区域采购在途',
          key: 'areaOnPurchase',
          width: 120
        },
        {
          title: '收货区域转货在途',
          key: 'areaTransferTotal',
          width: 120
        }
      ]
    },
    // 勾选需要处理的库存
    selectionChange (rows) {
      this.tableInfo.selection = rows;
      const indexList = rows.map(item => item.index)
      this.tableInfo.list.forEach(ele => {
        if (indexList.includes(ele.index)) {
          if (ele.canTransferNum >= ele.canSendAmount) {
            ele.transferNum = ele.canSendAmount
          } else if (ele.canTransferNum < ele.canSendAmount) {
            ele.transferNum = ele.canTransferNum
          }
        } else {
          ele.transferNum = 0
        }
      });
    },
    handleOpenDialog () {
      this.formData = {
        receiveCompanyName: '',
        productName: '',
        idList: [...new Set(this.selectIds)]
      }
      this.initTableColumn()
      this.getTableData()
    },
    // 关闭弹框
    handleCloseDialog () {
      this.$emit('update:companyVisible', false);
    },
    // 获取表格数据
    async getTableData () {
      const res = await queryTransferCompanyService(
        { ...this.formData },
        state => { this.tableInfo.loading = state }
      );
      if (res?.retData) {
        this.tableInfo.list = res.retData;
      } else {
        this.tableInfo.list = []
      }
    },
    handleConfirm () {
      const { selection } = this.tableInfo;
      if (selection.some(item => (!item.transferNum))) {
        return this.$message.warning('勾选项的转货数量不能为空')
      }
      this.$emit('setTransferNum', selection)
      this.handleCloseDialog()
    },
  }
};
</script>
